package com.power.powercash;

import com.db4o.query.Predicate;
import com.power.utils.Date;

/**
 *
 * @author f6036477
 */
public class ConsultaDate extends Predicate<Date> implements CompareDate
{

  private CompareDateMethod compare;

  private Date date, before;

  private boolean empty = false;


  public ConsultaDate()
  {
    compare = COMPARE_EQUALS;
    date = new Date();
    before = new Date();
  }


  public ConsultaDate(Date d)
  {
    compare = COMPARE_EQUALS;
    date = d;
    before = new Date();
  }


  public void setEmptyQuery(boolean emptyQuery)
  {
    empty = emptyQuery;
  }


  public boolean isEmptyQuery()
  {
    return empty;
  }


  @Override
  public CompareDateMethod getCompareDateMethod()
  {
    return compare;
  }


  @Override
  public void setCompareDateMethod(CompareDateMethod c)
  {
    compare = c;
  }


  public Date getDate()
  {
    return date;
  }


  public void setDate(Date d)
  {
    date = d;
  }


  public Date getDateBefore()
  {
    return before;
  }


  public void setDateBefore(Date d)
  {
    before = d;
  }


  @Override
  public boolean match(Date d)
  {
    if(d != null && empty) return true;
    return match(d, date);
  }


  @Override
  public boolean match(Date d1, Date d2)
  {
    if(d1 == null || d2 == null)
      return false;

    if(compare == COMPARE_EQUALS)
      return d1.equals(d2);
    else if(compare == COMPARE_BEFORE)
      return d1.isBefore(d2);
    else if(compare == COMPARE_AFTER)
      return d1.isAfter(d2);
    else {
      if(before == null) return false;
      return match(before, d1, d2);
    }
  }


  @Override
  public boolean match(Date d1, Date d, Date d2)
  {
    if(d1 == null || d == null || d2 == null)
      return false;

    return d1.isBefore(d) && d2.isAfter(d);
  }


  public static void main(String[] args)
  {
    Date d1 = Date.parseDate("01.07.2010");
    Date d = Date.parseDate("01.08.2010");
    Date d2 = Date.parseDate("01.09.2010");

    ConsultaDate cd = new ConsultaDate();
    System.out.println("d1: "+ d1.getDateStringFormat() + ", d: "+ d.getDateStringFormat() + ", d2: "+ d2.getDateStringFormat());

    System.out.println("cd.setCompareDateMethod(ConsultaDate.COMPARE_BEFORE);");
    cd.setCompareDateMethod(ConsultaDate.COMPARE_BEFORE);
    System.out.println("cd.match(d1, d): "+ cd.match(d1, d));
    System.out.println("cd.match(d, d1): "+ cd.match(d, d1));

    System.out.println("cd.setCompareDateMethod(COMPARE_BETWEEN);");
    cd.setCompareDateMethod(COMPARE_BETWEEN);
    System.out.println("cd.setDateBefore(d1);");
    cd.setDateBefore(d1);
    System.out.println("cd.setDate(d);");
    cd.setDate(d);
    System.out.println("cd.match(d1, d, d2): "+ cd.match(d1, d, d2));
    System.out.println("cd.match(d2): "+ cd.match(d2));

    System.out.println("cd.setCompareDateMethod(COMPARE_AFTER);");
    cd.setCompareDateMethod(COMPARE_AFTER);
    System.out.println("cd.setDate(d);");
    cd.setDate(d);
    System.out.println("cd.match(d1): "+ cd.match(d1));
    System.out.println("cd.match(d2): "+ cd.match(d2));
  }

}
